package com.situ.mall.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.category.spi.model.CategoryTreeDTO;
import com.mall.common.util.JsonResult;
import com.situ.mallbrand.model.Brand;
import com.situ.mall.web.service.BrandFeignService;
import com.situ.mall.web.service.CategoryFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 品牌页面控制器
 */
@Slf4j
@Controller
public class BrandPageController {

    @Autowired
    private BrandFeignService brandFeignService;
    
    @Autowired
    private CategoryFeignService categoryFeignService;

    /**
     * 品牌列表页面
     */
    @GetMapping("/brands")
    public String brands(Model model,
                        @RequestParam(value = "keyword", required = false) String keyword,
                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "size", defaultValue = "12") Integer size) {
        try {
            // 1. 获取分类树数据（用于导航）
            JsonResult<List<CategoryTreeDTO>> categoryResult = categoryFeignService.getCategoryTree(1);
            if (categoryResult != null && categoryResult.getData() != null) {
                model.addAttribute("categories", categoryResult.getData());
            }

            // 2. 获取品牌列表数据
            JsonResult<Page<Brand>> brandsResult = brandFeignService.list(
                    keyword, // 搜索关键词
                    1,       // 只获取启用的品牌
                    page,
                    size
            );

            if (brandsResult != null && brandsResult.getData() != null) {
                Page<Brand> brandPage = brandsResult.getData();
                model.addAttribute("brands", brandPage.getRecords());
                
                // 分页信息
                model.addAttribute("currentPage", page);
                model.addAttribute("pageSize", size);
                model.addAttribute("totalPages", (int) brandPage.getPages());
                model.addAttribute("total", brandPage.getTotal());
                
                log.info("获取品牌列表: 总数={}, 当前页={}, 页大小={}", brandPage.getTotal(), page, size);
            } else {
                model.addAttribute("brands", java.util.Collections.emptyList());
                model.addAttribute("currentPage", page);
                model.addAttribute("pageSize", size);
                model.addAttribute("totalPages", 0);
                model.addAttribute("total", 0);
            }

            // 3. 获取热门品牌（首页展示的品牌）
            try {
                JsonResult<Page<Brand>> hotBrandsResult = brandFeignService.list(
                        null, // 不限制关键词
                        1,    // 只获取启用的品牌
                        1,    // 第一页
                        8     // 取8个热门品牌
                );
                
                if (hotBrandsResult != null && hotBrandsResult.getData() != null) {
                    Page<Brand> hotBrandPage = hotBrandsResult.getData();
                    model.addAttribute("hotBrands", hotBrandPage.getRecords());
                }
            } catch (Exception e) {
                log.warn("获取热门品牌失败", e);
                model.addAttribute("hotBrands", java.util.Collections.emptyList());
            }

            // 4. 保存搜索条件到模型（用于页面回显和分页链接）
            model.addAttribute("keyword", keyword);

        } catch (Exception e) {
            log.error("品牌列表页面数据加载失败", e);
            model.addAttribute("brands", java.util.Collections.emptyList());
            model.addAttribute("hotBrands", java.util.Collections.emptyList());
            model.addAttribute("currentPage", page);
            model.addAttribute("pageSize", size);
            model.addAttribute("totalPages", 0);
            model.addAttribute("total", 0);
        }

        return "brands";
    }
} 